﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
namespace HF.WorkFlow.Template
{
    /// <summary>
    /// 数据访问类Operator。
    /// </summary>
    public class Operator
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "OperatorId", "WorkFlowId", "WorkTaskId", "OperType", "OperContent", "Relation", "Description", "InorExclude", "OperDisplay" };
        private string tableName = "HF_Operator";
        private string keyField = "OperatorId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(OperatorEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@OperatorId", entity.OperatorId, typeof(string));
            sqlDataItem.AppendParameter("@WorkFlowId", entity.WorkFlowId, typeof(string));
            sqlDataItem.AppendParameter("@WorkTaskId", entity.WorkTaskId, typeof(string));
            sqlDataItem.AppendParameter("@OperType", entity.OperType, typeof(int));
            sqlDataItem.AppendParameter("@OperContent", entity.OperContent, typeof(string));
            sqlDataItem.AppendParameter("@Relation", entity.Relation, typeof(int));
            sqlDataItem.AppendParameter("@Description", entity.Description, typeof(string));
            sqlDataItem.AppendParameter("@InorExclude", entity.InorExclude, typeof(bool));
            sqlDataItem.AppendParameter("@OperDisplay", entity.OperDisplay, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public OperatorEntity GetOperatorEntity(DataRow dr)
        {
            OperatorEntity entity = new OperatorEntity();
            string value = "";
            entity.OperatorId = dr["OperatorId"].ToString();
            entity.WorkFlowId = dr["WorkFlowId"].ToString();
            entity.WorkTaskId = dr["WorkTaskId"].ToString();
            value = dr["OperType"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.OperType = Convert.ToInt32(value);
            }
            entity.OperContent = dr["OperContent"].ToString();
            value = dr["Relation"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.Relation = Convert.ToInt32(value);
            }
            entity.Description = dr["Description"].ToString();
            value = dr["InorExclude"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.InorExclude = true;
                }
                else
                {
                    entity.InorExclude = false;
                }
            }
            entity.OperDisplay = dr["OperDisplay"].ToString();
            return entity;
        }

        #endregion

        #region  构造函数
        public Operator()
        { }
        public static Operator GetInstance()
        {
            return new Operator();
        }
        #endregion

        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(OperatorEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(OperatorEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string operatorId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, operatorId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="operatorId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string operatorId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, operatorId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="OperatorId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetOperatorTable(string operatorId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, operatorId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="operatorId">主键Id</param>
        /// <returns>DataTable</returns>
        public OperatorEntity GetOperatorEntity(string operatorId)
        {
            DataTable dt = GetOperatorTable(operatorId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetOperatorEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion

        #region  扩展方法
        //此处添加扩展方法

        /// <summary>
        /// 获得处理者内容
        /// </summary>
        /// <param name="worktaskId">处理模板Id</param>
        /// <returns>处理者的信息</returns>
        public  string GetOperContent(string operatorId)
        {
            try
            {
                string tmpStr = "select OperContent from HF_Operator where OperatorId=@operatorId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@OperatorId", operatorId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteScalar(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得处理者的关系
        /// </summary>
        /// <param name="worktaskId">处理者Id</param>
        /// <returns></returns>
        public  string GetOperRelation(string OperatorId)
        {
            try
            {
                string tmpStr = "select Relation from HF_Operator where OperatorId=@OperatorId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@OperatorId", OperatorId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteScalar(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得任务节点的所有处理者
        /// </summary>
        /// <param name="workTaskId">任务模板Id</param>
        /// <returns>DataTable</returns>
        public  DataTable GetOperatorsOfTask(string workTaskId)
        {
            try
            {
                string sqlStr = "select * from HF_Operator where WorkTaskId =@workTaskId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@workTaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 被转发的流程
        /// </summary>
        /// <param name="worktaskinId"></param>
        /// <returns></returns>
        public string GetOperConTextWorktask(string worktaskinId)
        {
            try
            {
                string tmpStr = "select WorkTaskId from HF_WorkTaskInstanceView where WorkTaskInstanceId in(select PreviousTaskInstanceId from HF_WorkTaskInstanceView WHERE WorkTaskInstanceId=@worktaskinId)";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@worktaskinId", worktaskinId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteScalar(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        /// <summary>
        /// 被转发的流程
        /// </summary>
        /// <param name="worktaskinId"></param>
        /// <returns></returns>
        public string GetOperConText(string worktaskinId)
        {
            try
            {
                string tmpStr = "select WorkTaskId from HF_WorkTaskInstanceView where WorkTaskInstanceId in(select PreviousTaskInstanceId from HF_WorkTaskInstanceView WHERE WorkTaskInstanceId=@worktaskinId and OperType='12')";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@worktaskinId", worktaskinId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteScalar(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得任务节点的所有处理者
        /// </summary>
        /// <param name="workTaskId">任务模板Id</param>
        /// <returns>DataTable</returns>
        public List<OperatorEntity> GetOperatorEntitysOfTask(string workTaskId)
        {
            List<OperatorEntity> result = new List<OperatorEntity>();
            DataTable dt = GetOperatorsOfTask(workTaskId);
            try
            {
                foreach (DataRow dr in dt.Rows)
                {
                    OperatorEntity entity = GetOperatorEntity(dr);
                    result.Add(entity);
                }
                return result;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public  DataTable GetOperatorsOfFlow(string workFlowid)
        {
            try
            {
                string tmpStr = "select * from HF_Operator where  workflowId=@workflowId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@workflowId", workFlowid);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 判断用户是否排除处理
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="workTaskId">任务Id</param>
        /// <returns>是否排除处理</returns>
        public static bool IsExclude(string userId,string workTaskId)
        { 
            try
            {
                string tmpStr = "select * from HF_Operator where  worktaskId=@worktaskId and OperContent=@userId and InorExclude=@InorExclude";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@workTaskId", workTaskId);
                sqlItem.AppendParameter("@userId", userId);
                sqlItem.AppendParameter("@InorExclude",false,typeof(bool));
                ClientDBAgent agent = new ClientDBAgent();
                return agent.RecordExists(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion
    }
}

